/*
 * Copyright (C) 2010 Moduad Co., Ltd.
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package org.androidpn.server.console.controller;

import com.fujitsu.nanjing.mobile.common.CommonParamter;

import org.androidpn.server.util.Config;
import org.androidpn.server.xmpp.push.NotificationManager;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/** 
 * A controller class to process the notification related requests.  
 *
 * @author Sehwan Noh (devnoh@gmail.com)
 */
@Controller
public class NotificationController{
    /**
     * Logger for this class
     */
    private static final Logger logger = Logger
            .getLogger(NotificationController.class);
    @Resource
    private NotificationManager notificationManager;

    private final static int DEFAULT_SEND_ID=0;
    private final static int AUTO_INCREMENT=0;
    
    private String apiKey = Config.getString("apiKey", "");

    public NotificationController() {
    }
    @RequestMapping(value="/notification.do")
    public ModelAndView list(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        ModelAndView mav = new ModelAndView();
        // mav.addObject("list", null);
        mav.setViewName("notification/form");
        return mav;
    }
    @RequestMapping(value="/notification.do/send")
    public ModelAndView send(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        String broadcast = ServletRequestUtils.getStringParameter(request,
                "broadcast", "N");
        String username = ServletRequestUtils.getStringParameter(request,
                "username");
        String title = ServletRequestUtils.getStringParameter(request,"title",CommonParamter.DEFAULT_MSG_TITLE_NOTICE);
        String message = ServletRequestUtils.getStringParameter(request,
                "message","message");
        String msgid = ServletRequestUtils.getStringParameter(request, "msgid","0");
        if(msgid.equals("0")){
            if(broadcast.equals("N")){
//            BpoUcenter ucenter = bpoUcenterService.getUserByUserName(username);
//            SystemMessage t = new SystemMessage(AUTO_INCREMENT, message, DEFAULT_SEND_ID, ucenter.getId(),
//                    ucenter.getRoleId(), ucenter.getBigName(),
//                    ucenter.getSmallName(), ReadState.unRead.getState(),
//                    new Date());
//            systemMessageService.add(t);
//            msgid = String.valueOf(t.getId());
//            }
//            else{
//                List<BpoUcenter> ucenters=bpoUcenterService.getAllUsers();
//                //群发
//                sendMessage2Users(ucenters, message, title);
        }
        }
        logger.info("title="+title);
        logger.info("message="+message);
        logger.debug("apiKey=" + apiKey);

        if (broadcast.equalsIgnoreCase("Y")) {
            notificationManager.sendBroadcast(apiKey, title, message, msgid);
        } else {
            notificationManager.sendNotifcationToUser(apiKey, username, title,
                    message, msgid);
        }

        ModelAndView mav = new ModelAndView();
        mav.setViewName("redirect:/notification.do");
        return mav;
    }
    
//    private void sendMessage2Users(List<BpoUcenter> ucenters,String message,String title){
//        for(BpoUcenter ucenter:ucenters){
//        SystemMessage t = new SystemMessage(AUTO_INCREMENT, message, DEFAULT_SEND_ID, ucenter.getId(),
//                ucenter.getRoleId(), ucenter.getBigName(),
//                ucenter.getSmallName(), ReadState.unRead.getState(),
//                new Date());
//        systemMessageService.add(t);
//       String  msgid = String.valueOf(t.getId());
//       notificationManager.sendNotifcationToUser(apiKey, ucenter.getUsername(), title,
//               message, msgid);
//        }
//    }

}
